package org.example.springboot.mapper;

import org.apache.ibatis.annotations.*;
import org.example.springboot.entity.News;
import java.util.List;

@Mapper
public interface NewsMapper {

    @Select("SELECT * FROM news WHERE title LIKE CONCAT('%', #{title}, '%') LIMIT #{offset}, #{pageSize}")
    List<News> getNewsByPage(@Param("pageSize") int pageSize, @Param("offset") int offset, @Param("title") String title);

    @Select("SELECT COUNT(*) FROM news WHERE title LIKE CONCAT('%', #{title}, '%')")
    Integer getNewsCount(@Param("title") String title);

    @Delete("DELETE FROM news WHERE id = #{id}")
    void deleteNewsById(@Param("id") int id);

    @Insert("INSERT INTO news (avatar, title, content, link) " +
            "VALUES (#{avatar}, #{title}, #{content}, #{link})")
    void insertNews(News news);

    @Select("SELECT * FROM news WHERE id = #{id}")
    News selectById(@Param("id") int id);

    @Update("UPDATE news SET avatar = #{avatar}, title = #{title}, content = #{content}, link = #{link} WHERE id = #{id}")
    void updateNews(News news);

    @Select("SELECT * FROM news")
    List<News> selectAll();


}
